package com.yudean.esb.dao.esb;

import com.yudean.esb.domain.esb.EsbServiceConfig;
import com.yudean.esb.domain.esb.EsbServiceConfigRelation;
import com.yudean.esb.domain.esb.EsbServiceMapper;
import com.yudean.esb.domain.esb.EsbServiceSubMapper;
import com.yudean.esb.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Kaibin
 * Date: 13-10-23
 * Time: 上午9:02
 */
@Repository("esbServiceHelperDao")
public class EsbServiceHelperDao {

    public static final Logger logger = LoggerFactory.getLogger(EsbServiceHelperDao.class);

    private String primaryTableName;
    private Map<String, String> primaryMap = new HashMap<String, String>();
    private Map<String, String> subMap = new HashMap<String, String>();

    @Resource
    EsbServiceConfigDao esbServiceConfigDao;
    @Resource
    EsbServiceMapperDao esbServiceMapperDao;
    @Resource
    EsbServiceConfigRelationDao esbServiceConfigRelationDao;
    @Resource
    EsbServiceSubMapperDao esbServiceSubMapperDao;

    /**
     * 根据服务名获取主表名
     *
     * @param serviceID
     * @return
     */
    public String getTableNameByServiceID(String serviceID) {
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig != null) {
            return esbServiceConfig.getTbName();
        }
        return null;
    }

    /**
     * 根据服务名获取主表的列名和发布节点名映射
     * key：列名
     * value：发布节点名
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getPrimaryServiceMap(String serviceID) {
        Map<String, String> primaryServiceMap = new HashMap<String, String>();
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);      //ESB_SERVICE_CONFIG
        if (esbServiceConfig != null) {
            EsbServiceMapper filter = new EsbServiceMapper();
            filter.setServiceID(esbServiceConfig.getID());
            filter.setMapperType("P");
            List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
            if (list != null && list.size() > 0) {
                for (EsbServiceMapper mapper : list) {
                    primaryServiceMap.put(mapper.getTbColName(), mapper.getNodeName());         //ESB_SERVICE_MAPPER 列名(TB_COL_NAME)——》属性名(NODE_NAME)
                }
            }
        }
        return primaryServiceMap;
    }

    /**
     * 根据服务名获取主表的节点名和数据类型
     * key：列名
     * value：字段类型
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getPrimaryServiceDataTypeMap(String serviceID) {
        Map<String, String> primaryServiceMap = new HashMap<String, String>();
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig != null) {
            EsbServiceMapper filter = new EsbServiceMapper();
            filter.setServiceID(esbServiceConfig.getID());
            filter.setMapperType("P");
            List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
            if (list != null && list.size() > 0) {
                for (EsbServiceMapper mapper : list) {
                    //ESB_SERVICE_MAPPER ——> TB_COL_NAME DATA_TYPE
                    primaryServiceMap.put(mapper.getNodeName(), mapper.getDataType());
                }
            }
        }
        return primaryServiceMap;
    }

    /**
     * 根据服务名获取主表的权限列节点名和数据类型
     * key：列名
     * value：字段类型
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getPrimaryServiceAuthorityFieldMap(String serviceID) {
        Map<String, String> primaryServiceMap = new HashMap<String, String>();
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig != null) {
            EsbServiceMapper filter = new EsbServiceMapper();
            filter.setServiceID(esbServiceConfig.getID());
            filter.setMapperType("P");
            filter.setIsAuthorityField("1");
            List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
            if (list != null && list.size() > 0) {
                for (EsbServiceMapper mapper : list) {
                    //COMP_CODE、SOURCE_CODE
                    primaryServiceMap.put(mapper.getTbColName(), mapper.getDataType());
                }
            }
        }
        return primaryServiceMap;
    }

    /**
     * 根据服务名获取主表的权限列节点名和数据类型
     * key：列名
     * value：字段类型
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getPrimaryServiceQueryableFieldMap(String serviceID) {
        Map<String, String> primaryServiceMap = new HashMap<String, String>();
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig != null) {
            EsbServiceMapper filter = new EsbServiceMapper();
            filter.setServiceID(esbServiceConfig.getID());
            filter.setMapperType("P");
            filter.setIsQueryField("1");
            List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
            if (list != null && list.size() > 0) {
                for (EsbServiceMapper mapper : list) {
                    primaryServiceMap.put(mapper.getNodeName(), mapper.getDataType());
                }
            }
        }
        return primaryServiceMap;
    }

    /**
     * 根据服务名获取聚合属性和关联子表的映射
     * key：聚合属性名
     * value：关联子表名
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getPolymericServiceMap(String serviceID) {
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);  //表：ESB_SERVICE_CONFIG
        if (esbServiceConfig == null) {
            return null;
        }
        EsbServiceMapper filter = new EsbServiceMapper();
        filter.setServiceID(esbServiceConfig.getID());
        filter.setMapperType("S");
        List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);      //esb_service_mapper
        if (list == null || list.size() <= 0) {
            return null;
        }
        Map<String, String> polymericServiceMap = new HashMap<String, String>();
        for (EsbServiceMapper map : list) {
            EsbServiceConfigRelation relation = new EsbServiceConfigRelation();
            relation.setServiceMapperID(map.getID());
            List<EsbServiceConfigRelation> relations = esbServiceConfigRelationDao.get(relation);
            if (relations != null && relations.size() > 0) {
                EsbServiceConfigRelation r = relations.get(0);
                polymericServiceMap.put(map.getNodeName(), r.getSubTbName());   //contract  IDS_AP_CONTRACT_INT
            }                                                                   //payment   IDS_AP_CON_PAYMENTS_INT
        }
        return polymericServiceMap;
    }

    /**
     * 根据服务名获取子表的列名和发布节点名映射,
     * 可能有多个子表关联，返回key为聚合属性名，value为子表的列名和发布节点名的映射
     *
     * @param serviceID
     * @return
     */
    public Map<String, Map<String, String>> getSubServiceMapMap(String serviceID) {
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig == null) {
            return null;
        }
        EsbServiceMapper filter = new EsbServiceMapper();
        filter.setServiceID(esbServiceConfig.getID());
        filter.setMapperType("S");
        List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
        if (list == null || list.size() <= 0) {
            return null;
        }
        Map<String, Map<String, String>> subServiceMapMap = new HashMap<String, Map<String, String>>();
        for (EsbServiceMapper map : list) {
            Map<String, String> subServiceMap = new HashMap<String, String>();
            EsbServiceConfigRelation relation = new EsbServiceConfigRelation();
            relation.setServiceMapperID(map.getID());
            List<EsbServiceConfigRelation> relations = esbServiceConfigRelationDao.get(relation);     //tableName:ESB_SERVICE_CONFIG_RELATION
            if (relations != null && relations.size() > 0) {
                EsbServiceConfigRelation r = relations.get(0);
                List<EsbServiceSubMapper> subList = esbServiceSubMapperDao.getByRelationID(r.getID());
                for (EsbServiceSubMapper subMapper : subList) {
                    subServiceMap.put(subMapper.getTbColName(), subMapper.getNodeName());                //tableName:ESB_SERVICE_SUB_MAPPER
                }
            }
            subServiceMapMap.put(map.getNodeName(), subServiceMap);//key : ESB_SERVICE_MAPPER.NODE_NAME
        }
        return subServiceMapMap;
    }

    /**
     * 根据服务名获取子表的列名和关联主表列映射
     * key:子表列名
     * value：关联主表列名
     *
     * @param serviceID
     * @return
     */
    public Map<String, String> getSubRelationColMap(String serviceID) {
        Map<String, String> subRelationColMap = new HashMap<String, String>();
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig == null) {
            return null;
        }
        EsbServiceMapper filter = new EsbServiceMapper();
        filter.setServiceID(esbServiceConfig.getID());
        filter.setMapperType("S");
        List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
        if (list != null && list.size() > 0) {
            for (EsbServiceMapper map : list) {
                EsbServiceConfigRelation relation = new EsbServiceConfigRelation();
                relation.setServiceMapperID(map.getID());
                List<EsbServiceConfigRelation> relations = esbServiceConfigRelationDao.get(relation);
                if (relations != null && relations.size() > 0) {
                    EsbServiceConfigRelation r = relations.get(0);
                    List<EsbServiceSubMapper> subList = esbServiceSubMapperDao.getByRelationID(r.getID());
                    for (EsbServiceSubMapper subMapper : subList) {
                        if (!StringUtil.isEmpty(subMapper.getRelationColName())) {
                            subRelationColMap.put(subMapper.getTbColName(), subMapper.getRelationColName());
                        }
                    }
                }
            }
        }
        return subRelationColMap;
    }


    public Map<String, Map<String, String>> getSubRelationMapMap(String serviceID) {
        Map<String, Map<String, String>> subRelationMap = new HashMap<String, Map<String, String>>();
        //select * from esb_service_config t where t.service_id = 'erp_ids_payments';
        EsbServiceConfig esbServiceConfig = esbServiceConfigDao.getByServiceID(serviceID);
        if (esbServiceConfig == null) {
            return null;
        }
        EsbServiceMapper filter = new EsbServiceMapper();
        filter.setServiceID(esbServiceConfig.getID());
        filter.setMapperType("S");
        //select * from esb_service_mapper t where t.service_id = 'dd0827d4055a44de87b5591f95baf13b' and t.mapper_type = 'S';
        List<EsbServiceMapper> list = esbServiceMapperDao.get(filter);
        if (list != null && list.size() > 0) {
            for (EsbServiceMapper map : list) {
                EsbServiceConfigRelation relation = new EsbServiceConfigRelation();
                relation.setServiceMapperID(map.getID());
                //select * from esb_service_config_relation t where t.service_mapper_id = '7bad7775ca904f04b12fcfb04312160d';
                List<EsbServiceConfigRelation> relations = esbServiceConfigRelationDao.get(relation);
                Map<String, String> colMap = new HashMap<String, String>();
                if (relations != null && relations.size() > 0) {
                    EsbServiceConfigRelation r = relations.get(0);
                    //select * from ESB_SERVICE_SUB_MAPPER t where t.relation_id = 'b09fab136b9e46eda54656ba16dfd65d';
                    List<EsbServiceSubMapper> subList = esbServiceSubMapperDao.getByRelationID(r.getID());
                    for (EsbServiceSubMapper subMapper : subList) {
                        if (!StringUtil.isEmpty(subMapper.getRelationColName())) {                  // ESB_SERVICE_SUB_MAPPER TB_COL_NAME  RELATION_COL_NAME
                            colMap.put(subMapper.getTbColName(), subMapper.getRelationColName());
                        }
                    }
                }
                subRelationMap.put(map.getNodeName(), colMap);
            }
        }
        return subRelationMap;
    }
}
